/**
 * 
 */
package org.xelnaga.defiler.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.xelnaga.defiler.domain.ContentletModel;
import org.xelnaga.defiler.domain.ContentletWrapper;
import org.xelnaga.defiler.domain.FieldModel;
import org.xelnaga.defiler.domain.FileAssetModel;
import org.xelnaga.defiler.domain.FolderModel;
import org.xelnaga.defiler.domain.CouponModel;
import org.xelnaga.defiler.service.ContentletService;
import org.xelnaga.defiler.service.FieldService;
import org.xelnaga.defiler.service.FileAssetService;
import org.xelnaga.defiler.service.FolderService;
import org.xelnaga.defiler.service.CouponService;
import org.xelnaga.defiler.web.page.mapping.CacheableViewController;

/**
 * @author qijb
 *
 */
@Controller("cacheableItemPageController")
public class CacheableItemPageController extends CacheableViewController {
	@Autowired
	private ContentletService contentletService;
	@Autowired
	private FieldService fieldService;
	@Autowired
	private FolderService folderService;
	@Autowired
	private FileAssetService fileAssetService;
	@Autowired
	private CouponService couponService;
	@Override
	protected void populateModel(Model model, HttpServletRequest request) {
		super.populateModel(model, request);
		String inode = request.getParameter("inode");
		if(StringUtils.isNotBlank(inode) && StringUtils.isNumeric(inode)) {
			ContentletModel contentlet = contentletService.getByPrimaryKey(Long.valueOf(inode));
			List<FieldModel> list = fieldService.getByStructureInode(contentlet.getStructureInode());
			model.addAttribute("contentlet", new ContentletWrapper(list, contentlet));
			 
			List<FileAssetModel> fileList = fileAssetService.getCoverListByContentlet(contentlet);
			model.addAttribute("fileAssetList", fileList);
			
			//面包屑
			List<FolderModel> ancestorList = folderService.getAncestorListByInode(contentlet.getFolderInode(), true);
			model.addAttribute("ancestorList", ancestorList);
			
			List<CouponModel> coupons = couponService.getListByContentlet(contentlet);
			model.addAttribute("couponList", coupons);
		}
	}

}
